<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodsController extends Controller
{
    public function getAdd()
    {
    

       $alldata=DB::table('cate')
            ->select( '*',DB::raw("concat(path,',',id) as apath"))
            ->orderBy("apath")
            ->get();
     
       foreach($alldata as $k =>$v)
       {
        $num=substr_count($v->path,',');
        $alldata[$k]->name=str_repeat('|----', $num).$v->name;
       }



        return  view('admin.goods.add',['alldata'=>$alldata]);
    	
    }

//添加
    public function postInsert(Request $request)
    {
    	$data=$request->except('_token');
    	// dd($data);
    	if($request->hasFile('img')) 
    	{
    	
    		if($request->file('img')->isValid())

    		{
    			
    			$ext=$request->file('img')->getClientOriginalExtension();
    			$filename=time().mt_rand(100000,999999).'.'.$ext;
    			$request->file('img')->move('./uploads',$filename);
    			$data['img']=$filename;
    		}
    	}
    	 
    	 
    	 
    	$data['status']=0;
    	$res=DB::table('goods')->insert($data);




    	if($res)
        {
            return redirect('/admin/goods/index')->with(['info'=>'添加成功']);
        }else
        {
            return back()->with(['info'=>'添加失败']);
        }
    }
//添加
//商品主页
    public function getIndex(Request $request)
    {
    	
    	$data=DB::table('goods')
    				->select('goods.*','cate.name as cname')
    				->leftJoin('cate','goods.cate_id','=','cate.id')
					->where("goods.name","like","%".$request->input('search')."%" )
					->paginate($request->input('pages',10));



    	return view('admin.goods.index',['data'=>$data,'request'=>$request->all()]);

    }
//商品主页
//编辑
   public function getEdit($id)
   {
   	  $data=DB::table('goods')->where('id',$id)->first();

       $alldata=DB::table('cate')
            ->select( '*',DB::raw("concat(path,',',id) as apath"))
            ->orderBy("apath")
            ->get();
     
       foreach($alldata as $k =>$v)
       {
        $num=substr_count($v->path,',');
        $alldata[$k]->name=str_repeat('|----', $num).$v->name;
       }


   	  if($data)
		{
			return view('admin.goods.edit',['data'=>$data,'alldata'=>$alldata]);
		}else
		{

			return back()->with(['info'=>'编辑错误']);
		}
   	  
   }
//编辑
//编辑修改
	public function postUpdate(Request $request)
	{
		$data=$request->except('_token');
		$id=$request->id;
		$old=DB::table('goods')->where('id',$id)->first();
		$oldimg='./uploads/'.$old->img;
		
	
		if($request->hasFile('img'))
		{
			if($request->file('img')->isValid())
			{

				if (file_exists($oldimg)) {
					unlink($oldimg);
				}
				


				$ext=$request->file('img')->getClientOriginalExtension();
    			$filename=time().mt_rand(100000,999999).'.'.$ext;
    			$request->file('img')->move('./uploads',$filename);
    			$data['img']=$filename;

			}
		}
		$res=DB::table('goods')->where('id',$id)->update($data);
    	if($res)
        {
            return redirect('/admin/goods/index')->with(['info'=>'修改成功']);
        }else
        {
            return back()->with(['info'=>'修改失败']);
        }
	}
//编辑修改
//删除
	public function getDelete($id)
	{
		$img=DB::table('goods')->where('id',$id)->first()->img;
		$oldimg='./uploads/'.$img;
		$res=DB::table('goods')->delete($id);
		if($res)
        {
        	if (file_exists($oldimg)) {
					unlink($oldimg);
				}
            return redirect('/admin/goods/index')->with(['info'=>'删除成功']);
        }else
        {
            return back()->with(['info'=>'删除失败']);
        }

	}
//删除
//

}
